Adding some more judges, here and there.
[and.git] / SPOJ / EPALIN - 4103. Extend to Palindrome / epalin.cpp
blobbd66f7bd8fe12338b27d1239539875c6a2ed376b
1 // Andrés Mejía
2 using namespace std;
3 #include <algorithm>
4 #include <iostream>
5 #include <iterator>
6 #include <numeric>
7 #include <sstream>
8 #include <fstream>
9 #include <cassert>
10 #include <climits>
11 #include <cstdlib>
12 #include <cstring>
13 #include <string>
14 #include <cstdio>
15 #include <vector>
16 #include <cmath>
17 #include <queue>
18 #include <deque>
19 #include <stack>
20 #include <list>
21 #include <map>
22 #include <set>
24 #define foreach(x, v) for (typeof (v).begin() x=(v).begin(); x !=(v).end(); ++x)
25 #define For(i, a, b) for (int i=(a); i<(b); ++i)
26 #define D(x) cout << #x " is " << x << endl
28 const double EPS = 1e-9;
29 int cmp(double x, double y = 0, double tol = EPS) {
30 return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
33 const int MAXN = 100005 * 2;
35 int p[MAXN];
37 int main(){
38 string s, t;
39 s.reserve(MAXN);
40 t.reserve(MAXN);
42 while (getline(cin, s)) {
43 int n = s.size();
44 t = s + "$" + s;
45 reverse(t.begin(), t.begin() + n);
47 p[0] = 0;
48 for (int i = 1; i < 2 * n + 1; ++i) {
49 p[i] = p[i - 1];
50 while (p[i] > 0 and t[i] != t[p[i]]) p[i] = p[p[i] - 1];
51 if (t[i] == t[p[i]]) p[i]++;
54 // puts("");
55 // D(t);
56 // for (int i = 0; i < 2 * n + 1; ++i) {
57 // printf("%d ", p[i]);
58 // }
59 // puts("");
61 printf("%s", s.c_str());
62 for (int i = 0; i <= n; ++i) {
63 if (2 * i - n >= 0 and p[n + i] >= n - i) {
64 //printf("i = %d works on the first case\n", i);
65 for (int k = 2 * i - n - 1; k >= 0; --k) {
66 putchar(s[k]);
68 break;
71 if (i < n and 2 * i - n + 1 >= 0 and p[n + i + 1] >= n - i) {
72 for (int k = 2 * i - n; k >= 0; --k) {
73 putchar(s[k]);
75 break;
78 puts("");
80 return 0;